package com.example.maillist;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    EditText et_name,et_phone;
    Button btn_add,btn_del,btn_update,btn_show,btn_showAll;
    TextView tv_show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_name = findViewById(R.id.et_name);
        et_phone = findViewById(R.id.et_phone);
        btn_add = findViewById(R.id.btn_add);
        btn_del = findViewById(R.id.btn_del);
        btn_update = findViewById(R.id.btn_update);
        btn_show = findViewById(R.id.btn_show);
        btn_showAll = findViewById(R.id.btn_showAll);
        tv_show = findViewById(R.id.tv_show);

        btn_add.setOnClickListener(this);
        btn_showAll.setOnClickListener(this);
        btn_show.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_update.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //显示全部
            case R.id.btn_showAll :
                showAll();
                break;
            //添加
            case R.id.btn_add:
                add();
                break;
            //删除
            case R.id.btn_del:
                del();
                break;
            //修改
            case R.id.btn_update:
                update();
                break;
            //根据名称显示
            case R.id.btn_show:
                show();
                break;
        }
    }

    //根据名称显示
    private void show() {
        StringBuffer sb = new StringBuffer();
        String name = et_name.getText().toString();
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("contacttb", null, "name=?", new String[]{name},
                null, null, null, "1");
        while (cursor.moveToNext()){
            sb.append(cursor.getInt(0)+" "+cursor.getString(1)+" "+cursor.getString(2));
        }
        String msg = sb.toString();
        tv_show.setText(msg);
        cursor.close();
        db.close();
    }

    //修改
    private void update() {
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("tel",et_phone.getText().toString());
        db.update("contacttb",values,"name=?",new String[]{et_name.getText().toString()});
        Toast.makeText(this, "修改成功！", Toast.LENGTH_SHORT).show();
        showAll();
    }

    //删除
    private void del() {
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
            SQLiteDatabase db = helper.getWritableDatabase();
            db.delete("contacttb","name=?",new String[]{et_name.getText().toString()});
            db.close();
        Toast.makeText(this, "删除成功！", Toast.LENGTH_SHORT).show();
            showAll();
    }

    //添加
    private void add() {

            MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
            SQLiteDatabase db = helper.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put("name",et_name.getText().toString());
            values.put("tel",et_phone.getText().toString());
            db.insert("contacttb",null,values);
            System.out.println(et_name.getText().toString()+"*****"+et_phone.getText().toString());
            db.close();
        Toast.makeText(this, "添加成功！", Toast.LENGTH_SHORT).show();
            showAll();
    }

    //显示全部
    private void showAll() {
        StringBuffer sb = new StringBuffer(); //存储数据
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("contacttb", null, null, null, null, null, null);
        while (cursor.moveToNext()){
            int _id = cursor.getInt(0);
            String name = cursor.getString(1);
            String tel = cursor.getString(2);
            sb.append(_id+"  "+name+"  "+tel+"\n");
        }
        String msg = sb.toString();
        tv_show.setText(msg);

        cursor.close();
        db.close();
    }
}
